<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.web.rest.errors;

import <%= packageName %>.IntegrationTest;
import org.hamcrest.core.AnyOf;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
<%_ if (authenticationUsesCsrf) { _%>
import org.junit.jupiter.api.BeforeEach;

<%_ } _%>
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
<%_ if (authenticationUsesCsrf) { _%>

import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf;
<%_ } _%>

/**
 * Integration tests {@link ExceptionTranslator} controller advice.
 */
@WithMockUser
@AutoConfigureWebTestClient(timeout = IntegrationTest.DEFAULT_TIMEOUT)
@IntegrationTest
class ExceptionTranslatorIT {

    @Autowired
    private WebTestClient webTestClient;
<%_ if (authenticationUsesCsrf) { _%>

    @BeforeEach
    void setupCsrf() {
        webTestClient = webTestClient.mutateWith(csrf());
    }
<%_ } _%>
<%_ if (!databaseTypeNo && !databaseTypeCassandra) { _%>

    @Test
    void testConcurrencyFailure() {
        webTestClient
            .get()
            .uri("/api/exception-translator-test/concurrency-failure")
            .exchange()
            .expectStatus()
            .isEqualTo(HttpStatus.CONFLICT)
            .expectHeader()
            .contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .expectBody()
            .jsonPath("$.message")
            .isEqualTo(ErrorConstants.ERR_CONCURRENCY_FAILURE);
    }
<%_ } _%>

    @Test
    void testMethodArgumentNotValid() {
        webTestClient
            .post()
            .uri("/api/exception-translator-test/method-argument")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue("{}")
            .exchange()
            .expectHeader()
            .contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .expectBody()
            .jsonPath("$.message")
            .isEqualTo(ErrorConstants.ERR_VALIDATION)
            .jsonPath("$.fieldErrors.[0].objectName")
            .isEqualTo("test")
            .jsonPath("$.fieldErrors.[0].field")
            .isEqualTo("test")
            .jsonPath("$.fieldErrors.[0].message")
            .isEqualTo("must not be null");
    }

    @Test
    void testMissingRequestPart() {
        webTestClient
            .get()
            .uri("/api/exception-translator-test/missing-servlet-request-part")
            .exchange()
            .expectStatus()
            .isBadRequest()
            .expectHeader()
            .contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .expectBody()
            .jsonPath("$.message")
            .isEqualTo("error.http.400");
    }

    @Test
    void testMissingRequestParameter() {
        webTestClient
            .get()
            .uri("/api/exception-translator-test/missing-servlet-request-parameter")
            .exchange()
            .expectStatus()
            .isBadRequest()
            .expectHeader()
            .contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .expectBody()
            .jsonPath("$.message")
            .isEqualTo("error.http.400");
    }

    @Test
    void testAccessDenied() {
        webTestClient
            .get()
            .uri("/api/exception-translator-test/access-denied")
            .exchange()
            .expectStatus()
            .isForbidden()
            .expectHeader()
            .contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .expectBody()
            .jsonPath("$.message")
            .isEqualTo("error.http.403")
            .jsonPath("$.detail")
            .isEqualTo("test access denied!");
    }

    @Test
    void testUnauthorized() {
        webTestClient
            .get()
            .uri("/api/exception-translator-test/unauthorized")
            .exchange()
            .expectStatus()
            .isUnauthorized()
            .expectHeader()
            .contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .expectBody()
            .jsonPath("$.message")
            .isEqualTo("error.http.401")
            .jsonPath("$.path")
            .isEqualTo("/api/exception-translator-test/unauthorized")
            .jsonPath("$.detail")
            .value(AnyOf.anyOf(IsEqual.equalTo("test authentication failed!"), IsEqual.equalTo("Invalid credentials")));
    }

    @Test
    void testMethodNotSupported() {
        webTestClient
            .post()
            .uri("/api/exception-translator-test/access-denied")
            .exchange()
            .expectStatus()
            .isEqualTo(HttpStatus.METHOD_NOT_ALLOWED)
            .expectHeader()
            .contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .expectBody()
            .jsonPath("$.message")
            .isEqualTo("error.http.405")
            .jsonPath("$.detail")
            .isEqualTo("405 METHOD_NOT_ALLOWED \"Request method 'POST' is not supported.\"");
    }

    @Test
    void testExceptionWithResponseStatus() {
        webTestClient
            .get()
            .uri("/api/exception-translator-test/response-status")
            .exchange()
            .expectStatus()
            .isBadRequest()
            .expectHeader()
            .contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .expectBody()
            .jsonPath("$.message")
            .isEqualTo("error.http.400")
            .jsonPath("$.title")
            .isEqualTo("test response status");
    }

    @Test
    void testInternalServerError() {
        webTestClient
            .get()
            .uri("/api/exception-translator-test/internal-server-error")
            .exchange()
            .expectHeader()
            .contentType(MediaType.APPLICATION_PROBLEM_JSON)
            .expectBody()
            .jsonPath("$.message")
            .isEqualTo("error.http.500")
            .jsonPath("$.title")
            .isEqualTo("Internal Server Error");
    }

}
